package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.player.CrossUserBaseInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C_UNION_DELETE, desc = "解散联盟,进入冷静期")
public class CrossDeleteUnionCmd extends CrossCommand {

    private long playerId;
    private int canDeleteUnionRet;//解散时的活动检查结果
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        playerId = packet.getPlayerId();
        ret = 0;
        UnionProto.CrossUnionDeleteReqMsg reqMsg = UnionProto.CrossUnionDeleteReqMsg.parseFrom(packet.getBytes());
        canDeleteUnionRet = reqMsg.getCanDeleteUnionRet();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        long nowTime = System.currentTimeMillis() / 1000;
        CrossUserBaseInfo baseInfo = CrossUserMgr.getCrossUserBaseInfo(playerId);
        if(baseInfo == null){
            ret = GameErrorCode.E_USER_NO_FOUND;
        }else {
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_UNION_DELETE, CrossUnionMgr.deleteUnion(baseInfo, nowTime, canDeleteUnionRet)));
        }
    }

    @Override
    public void sendData() {
        if (0 != ret) {
            UnionProto.UnionDeleteRespMsg.Builder respMsg = UnionProto.UnionDeleteRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setDeleteCoolingOffTime(0);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_UNION_DELETE, respMsg));
        }
    }
}
